package com.cetccst.securitysuite.activity;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static com.cetccst.securitysuite.kv.KVKeys.KEY_TEST;
import static com.cetccst.securitysuite.kv.SSKV.kvStore;
import static com.cetccst.securitysuite.log.SSLogger.logger;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import java.util.List;
import java.util.UUID;

//import cn.com.cetccst.activation.ActivationManager;
//import cn.com.cetccst.datacenter.collection.SSRespository;
//import cn.com.cetccst.datacenter.entity.TestEntity;

import com.cetccst.securitysuite.R;
import com.permissionx.guolindev.PermissionX;

import cn.com.cetccst.activation.ActivationManager;
import cn.com.cetccst.activation.ui.ScanQRActivity;
import cn.com.cetccst.tools.pm.AppPackage;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        logger.info("test test the first acitivity");
        setContentView(R.layout.activity_main);
        logger.info("the value is {}", kvStore.decodeString(KEY_TEST));
        kvStore.encode(KEY_TEST, "MainActivity");
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void onResume() {
        super.onResume();

        AppPackage.requestPermission(this,
                getString(cn.com.cetccst.activation.R.string.necessary_permissions_des), (allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                        if (!ActivationManager.get().isActivated()) {
                            ActivationManager.get().startActivation(this, (success, message) -> {
                                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                                getApplicationContext().startActivity(intent);
                            });
                            finish();
                        }
                    } else
                        finish();
                },
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.READ_PHONE_NUMBERS,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION);

        //TODO Manifest.permission.ACCESS_BACKGROUND_LOCATION  后续再优化


    }
}